package com.atguigu.spzx.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.spzx.common.exception.GuiguException;
import com.atguigu.spzx.listener.ExcelListener;
import com.atguigu.spzx.mapper.CategoryMapper;
import com.atguigu.spzx.model.entity.product.Category;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.product.CategoryExcelVo;
import com.atguigu.spzx.service.CategoryService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> findByParentId(Long parentId) {
        //查询当前第一层的分类
        List<Category> categoryList = categoryMapper.selectParentList(parentId);
        //判断集合是否为空
        if (!CollectionUtils.isEmpty(categoryList)) {
            //如果不为空,循环判断当前分类是否存在下一层分类
            categoryList.forEach(item -> {
                int count = categoryMapper.selectCategroyById(item.getId());
                if (count > 0) {
                    item.setHasChildren(true);
                } else {
                    item.setHasChildren(false);
                }
            });


        }
        return categoryList;
    }

    @Override
    public void exportData(HttpServletResponse response) {
        //导出数据
        try {
            //设置响应结果
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            //设置导出的地址
            String fileName = URLEncoder.encode("分类数据", StandardCharsets.UTF_8);
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            //查询所有数据
            List<Category> categoryList = categoryMapper.findAll();
            List<CategoryExcelVo> categoryExcelVoList = new ArrayList<>(categoryList.size());
            //将查询的对象转换为CategoryExcelVo
            if(!CollectionUtils.isEmpty(categoryList)){
                categoryList.forEach(item -> {
                    CategoryExcelVo categoryExcelVo = new CategoryExcelVo();
                    BeanUtils.copyProperties(item,categoryExcelVo);
                    categoryExcelVoList.add(categoryExcelVo);
                });
            }
            // 将数据写入到前端
            EasyExcel.write(response.getOutputStream(), CategoryExcelVo.class).sheet("分类数据").doWrite(categoryExcelVoList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void importData(MultipartFile file) {
        // 导入数据
        try {
            ExcelListener<Object> excelListener = new ExcelListener<>(categoryMapper);
            EasyExcel.read(file.getInputStream(),CategoryExcelVo.class,excelListener).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

    }
}
